package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.LoginDto;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-11-10
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    /**
     * 登录app校验
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //判断参数是否为空,为空则返回提示
        if(dto==null || StringUtils.isEmpty(dto.getPhone()) && StringUtils.isEmpty(dto.getPassword()) && StringUtils.isEmpty(dto.getEquipmentId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 不为空
        if(!StringUtils.isEmpty(dto.getPhone()) && !StringUtils.isEmpty(dto.getPassword())){
           //根据手机好查询用户
            LambdaQueryWrapper<ApUser> query=new LambdaQueryWrapper<>();
            query.eq(ApUser::getPhone,dto.getPhone());
            ApUser one = this.getOne(query);
            if(one==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
            //获取盐,并拼接MD5密码加密
            String salt = one.getSalt();
            String digest = DigestUtils.md5DigestAsHex((dto.getPassword()+salt).getBytes());
            //对比
            if(!digest.equals(one.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //定义
            Map<String,Object> userInfo=new HashMap<>();
            userInfo.put("userId",one.getId());
            String token = AppJwtUtil.getToken(userInfo);
            //定义前端token格式
            Map<String,Object> resultMap=new HashMap<>();
            //隐藏敏感
            one.setPassword(null);
            one.setSalt(null);
            resultMap.put("user",one);
            resultMap.put("token",token);
            return ResponseResult.okResult(resultMap);
        }
        // 设备编号不为空,默认为0,也能生成token
        if(!StringUtils.isEmpty(dto.getEquipmentId())){
            Map<String,Object> userMap=new HashMap<>();
            userMap.put("userId",0);
            String token = AppJwtUtil.getToken(userMap);
            Map<String,Object> result=new HashMap<>();
            result.put("token",token);
            return ResponseResult.okResult(result);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}
